<template>
  <div v-if="visible" class="footer-container">
    <div class="footer-content">
      <span class="footer-message">登录后，更懂你</span>
      <button class="login-button" @click="showDialog">立即登录</button>
      <span class="close-button" @click="closeFooter">X</span>
    </div>

    <!-- 登录模态框 -->
    <div v-if="loginDialogVisible" class="modal-overlay" @click.self="closeDialog">
      <div class="modal">
        <span class="modal-close-button" @click="closeDialog">X</span>
        <h2>登录</h2>
        <form @submit.prevent="processLogin">
          <div class="input-group">
            <input type="text" id="username" v-model="username" placeholder="请输入用户名" required>
          </div>
          <div class="input-group">
            <input :type="showPassword ? 'text' : 'password'" 
                   id="password" 
                   v-model="password" 
                   placeholder="请输入密码" 
                   required>
            <el-icon class="toggle-password" @click="togglePasswordVisibility">
              <View v-if="!showPassword" />
              <Hide v-else />
            </el-icon>
          </div>
          <div class="forgot-password-link">
            <a href="#" @click.prevent="showForgotPasswordDialog">忘记密码？</a>
          </div>
          <button type="submit" class="submit-button">登录</button>
          <div class="login-options">
            <span class="admin-entry" @click="navigateToAdminLogin">
              管理员入口
            </span>
            <span class="register-prompt" @click="showRegisterDialog">
              没有账号？点击注册
            </span>
          </div>
        </form>
      </div>
    </div>

    <!-- 忘记密码弹窗 -->
    <el-dialog
      v-model="forgotPasswordDialogVisible"
      title="重置密码"
      width="400px"
    >
      <div class="forgot-password-form">
        <el-input 
          v-model="forgetPasswordData.username"
          placeholder="请输入用户名"
        />
        <el-input 
          v-model="forgetPasswordData.newPassword"
          type="password"
          placeholder="请输入新密码"
          style="margin-top: 15px;"
        />
        <div class="dialog-footer">
          <el-button @click="forgotPasswordDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleResetPassword">确认重置</el-button>
        </div>
      </div>
    </el-dialog>

    <!-- 注册组件 -->
    <register-form 
      v-if="registerDialogVisible" 
      :visible="registerDialogVisible" 
      @close="registerDialogVisible = false"
    />
  </div>
</template>

<script>
import { defineComponent } from 'vue';
import { ElIcon, ElMessage } from 'element-plus';
import { Hide, View } from '@element-plus/icons-vue';
import axios from 'axios';
import RegisterForm from './RegisterForm.vue';

export default defineComponent({
  name: 'Mainmainfoots',
  components: {
    ElIcon,
    View,
    Hide,
    RegisterForm,
  },
  data() {
    return {
      visible: true,
      loginDialogVisible: false,
      registerDialogVisible: false,
      forgotPasswordDialogVisible: false,
      username: '',
      password: '',
      showPassword: false,
      forgetPasswordData: {
        username: '',
        newPassword: ''
      }
    };
  },
  methods: {
    // 显示/关闭对话框方法
    showDialog() { this.loginDialogVisible = true; },
    closeDialog() { 
      this.loginDialogVisible = false;
      this.registerDialogVisible = false;
    },
    
    // 密码可见性切换
    togglePasswordVisibility() {
      this.showPassword = !this.showPassword;
    },
    
    // 登录处理
    async processLogin() {
      try {
        const response = await axios.post('http://localhost:8092/user/login', {
          username: this.username,
          password: this.password
        });

        if (response.data.token) {
          const token = response.data.token;
          localStorage.setItem('token', token);
          axios.defaults.headers.common['Authorization'] = `Bearer ${token}`;
          ElMessage.success('登录成功');
          this.closeDialog();
          location.reload();
        } else if (response.data.error?.includes('锁定')) {
          ElMessage.error('账户已锁定，请10分钟后再试');
        } else {
          ElMessage.error(response.data.error || '登录失败');
        }
      } catch (error) {
        ElMessage.error('登录请求失败');
      }
    },

    // 忘记密码功能
    showForgotPasswordDialog() {
      this.forgotPasswordDialogVisible = true;
      this.loginDialogVisible = false;
    },
    async handleResetPassword() {
      if (!this.forgetPasswordData.username || !this.forgetPasswordData.newPassword) {
        ElMessage.error('请填写完整信息');
        return;
      }
      
      try {
        const params = new URLSearchParams();
        params.append('username', this.forgetPasswordData.username);
        params.append('newPassword', this.forgetPasswordData.newPassword);

        const response = await axios.post(
          'http://localhost:8092/user/forgot-password',
          params,
          { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
        );
        
        if (response.data.code === 200) {
          ElMessage.success('密码重置成功');
          this.forgotPasswordDialogVisible = false;
          this.forgetPasswordData = { username: '', newPassword: '' };
        } else {
          ElMessage.error(response.data.message || '密码重置失败');
        }
      } catch (error) {
        ElMessage.error('请求失败：' + error.message);
      }
    },

    // 其他功能
    navigateToAdminLogin() {
      const routeData = this.$router.resolve({ path: '/admin/login' });
      window.open(routeData.href, '_blank');
    },
    showRegisterDialog() {
      this.registerDialogVisible = true;
      this.loginDialogVisible = false;
    },
    closeFooter() {
      this.visible = false;
    },
  },
});
</script>

<style scoped>
.forgot-password-link {
  text-align: right;
  margin: 10px 0;
}

.forgot-password-link a {
  color: #007BFF;
  font-size: 14px;
  text-decoration: none;
}

.login-options {
  display: flex;
  justify-content: space-between;
  margin-top: 15px;
}

.admin-entry, .register-prompt {
  color: #007BFF;
  cursor: pointer;
  font-size: 14px;
}

.admin-entry:hover, .register-prompt:hover {
  text-decoration: underline;
}

.footer-container {
  position: fixed;
  bottom: 0;
  width: 100%;
  background-color: rgba(128, 128, 128, 0.7);
  padding: 20px 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

.footer-content {
  display: flex;
  align-items: center;
}

.footer-message {
  font-size: 20px;
  margin-right: 100px;
  color: white;
  font-weight: 500;
}

.login-button {
  background-color: yellow;
  color: black;
  border: none;
  border-radius: 25px;
  padding: 10px 20px;
  margin-right: 20px;
  cursor: pointer;
  font-size: 18px;
  font-weight: 500;
}

.close-button {
  font-size: 20px;
  cursor: pointer;
  color: #fff;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal {
  background-color: white;
  padding: 30px;
  border-radius: 8px;
  width: 400px;
  height: auto;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: relative;
}

.modal-close-button {
  position: absolute;
  top: 10px;
  right: 10px;
  font-size: 24px;
  cursor: pointer;
}

.input-group {
  margin-bottom: 20px;
  position: relative;
}

input[type="text"],
input[type="password"] {
  width: 100%;
  padding: 10px;
  padding-right: 40px;
  border: 1px solid #ccc;
  border-radius: 5px;
  font-size: 16px;
  box-sizing: border-box;
}

.toggle-password {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  font-size: 18px;
  color: grey;
}

.submit-button {
  background-color: #4CAF50;
  color: white;
  border: none;
  padding: 10px;
  cursor: pointer;
  border-radius: 5px;
  width: 100%;
}

.submit-button:hover {
  background-color: #45a049;
}

.register-prompt {
  margin-top: 15px;
  font-size: 14px;
  color: #007BFF;
  cursor: pointer;
  text-align: center;
}
</style>
